﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.GoodsModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmInventoryAlarm : Form
    {
        public FrmInventoryAlarm()
        {
            InitializeComponent();
        }

        GoodsManager goodsManager = new GoodsManager();
        int alarmNumber = 10;

        void SetHeaderText()
        {
            dgrdvAlarmGoods.Columns[0].HeaderText = "商品号";
            dgrdvAlarmGoods.Columns[0].Width = 168;
            dgrdvAlarmGoods.Columns[1].HeaderText = "商品名称";
            dgrdvAlarmGoods.Columns[1].Width = 200;
            dgrdvAlarmGoods.Columns[2].HeaderText = "进价";
            dgrdvAlarmGoods.Columns[2].Width = 100;
            dgrdvAlarmGoods.Columns[3].HeaderText = "售价";
            dgrdvAlarmGoods.Columns[3].Width = 100;
            dgrdvAlarmGoods.Columns[4].HeaderText = "库存量";
            dgrdvAlarmGoods.Columns[4].Width = 100;
        }

        private void FrmInventoryAlarm_Load(object sender, EventArgs e)
        {
            try
            {
                List<Goods> list = new List<Goods>();
                list = goodsManager.GetAlarmGoodss(alarmNumber);
                dgrdvAlarmGoods.DataSource = list;
                SetHeaderText();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgrdvAlarmGoods_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgrdvAlarmGoods.CurrentCell == null)
            {
                return;
            }
            int n = dgrdvAlarmGoods.CurrentCell.RowIndex;
            dgrdvAlarmGoods.Rows[n].Selected = true;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtInventory.Text.Trim() == "")
                {
                    MessageBox.Show("请输入库存量", "提示");
                    txtInventory.Focus();
                    return;
                }
                if (int.Parse(txtInventory.Text.Trim()) <= 0)
                {
                    MessageBox.Show("输入库存量必须大于零", "提示");
                    txtInventory.Text = "";
                    txtInventory.Focus();
                    return;
                }
                int number = int.Parse(txtInventory.Text.Trim());
                List<Goods> list = new List<Goods>();
                list = goodsManager.GetAlarmGoodss(number);
                dgrdvAlarmGoods.DataSource = list;
                SetHeaderText();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtInventory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSelect_Click(sender, e);
            }
        }

        private void btnAlarmGoods_Click(object sender, EventArgs e)
        {
            FrmInventoryAlarm_Load(sender, e);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
